package com.api.gbhg.liuwenbo.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.google.common.base.Joiner;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 独立核算单位财务
 */
@Path("/gb/budget")
public class IndependentAccountingApi {

    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();

    private Info[] paIf ;

    private String nd ;

    private String dwId ;
    /**
     * 获取 执行率、拨款、支付等数据
     * @return
     */
    @POST
    @Path("/getIAData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getData(Map<String,Object> param){

        if(param.containsKey("dw") && param.get("dw") instanceof Boolean && (Boolean)param.get("dw") == false)
            return JSON.toJSONString(null);

        init(param);
        //预算执行率
        setImplementRate();
        //中央财政拨款
        setCenterAppia(); 
        //地方财政拨款
        setlocalAppia();
        //中央转移支付
        setCenterTransfer();

        return JSON.toJSONString(paIf);
    }

    public void setDw(Map<String, Object> param){
        if(param.containsKey("dw") && param.get("dw") instanceof Integer ){
            this.dwId = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }else{
            this.dwId = "2"; //默认为2(拱北海关)
        }
    }



    /**
     * 获取项目执行情况统计数据
     * @return
     */
    @POST
    @Path("/getProjectSituation")
    @Produces(MediaType.APPLICATION_JSON)
    public String getProjectSituation(Map<String,Object> maps){

        if(maps.containsKey("dw") && maps.get("dw") instanceof Boolean && (Boolean)maps.get("dw") == false)
            return JSON.toJSONString(null);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

        Integer pageNum = 1;
        Integer pageSize = 5;
        String nd = sdf.format(new Date());
        String dw = "2";
        if(maps.containsKey("pageNum") && maps.get("pageNum") instanceof Integer &&(Integer)maps.get("pageNum")>0)
            pageNum = (Integer) maps.get("pageNum");
        if(maps.containsKey("pageSize") && maps.get("pageSize") instanceof Integer && (Integer)maps.get("pageSize")>0)
            pageSize = (Integer) maps.get("pageSize");
        if(maps.containsKey("nd") && maps.get("nd") instanceof Integer )
            nd = maps.get("nd").toString();
        if(maps.containsKey("dw") && maps.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) maps.get("dw"));
        }



        RecordSet rs = DataBaseUtil.getPjStRs(pageNum,pageSize,nd,dw,1);
        Map<String,Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<String,Object>();
            map[i].put("xmmc",rs.getString("xmmc"));
            RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdwRs.next();
            map[i].put("ssdw",sbdwRs.getString("SUBCOMPANYNAME"));
            map[i].put("ysje",rs.getString("ze"));
            map[i].put("zxl",AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"),rs.getDouble("ze")));
            i++;
        }
        com.api.gbhg.zengping.page.PageInfo<String> pageInfo=new com.api.gbhg.zengping.page.PageInfo<>();
        int cut = DataBaseUtil.getPjStCount(nd,dw,1);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(cut);
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);
    }


    /**
     * 预算流程
     * @param param
     * @return
     */
    @POST
    @Path("/getBudgetProcess")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetProcess(Map<String,Object> param){

        if(param.containsKey("dw") && param.get("dw") instanceof Boolean && (Boolean)param.get("dw") == false)
            return JSON.toJSONString(null);

        int nd = getNd(param);

        String dw = "2";
        if(param.containsKey("dw") && param.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));

        }

        Info[] budgetPs = new Info[2];
        //预算流程
        Map<String, Integer> cut = DataBaseUtil.getApprovalTask(nd, 1,dw);
        Map<String,Object>[] map = new HashMap[1];
        budgetPs[0] = new Info();
        map[0] = new HashMap<>();
        map[0].put("ycl",cut.get("processed"));
        map[0].put("wcl",cut.get("unprocessed"));
        budgetPs[0].setTitle("核批任务");
        budgetPs[0].setData(map);
        budgetPs[0].setId("hprw");

        //预算执行
        RecordSet bdilRs = DataBaseUtil.getBudgetImplementation(nd, 1,dw);
        budgetPs[1] = new Info();
        Map<String,Object>[]  bdMap = new HashMap[1];
        bdMap[0] = new HashMap<>();
        if((bdilRs.getDouble("ze") >=0 )){
            bdMap[0].put("yzlje", FormatUtil.DmFormat((bdilRs.getDouble("ze") - bdilRs.getDouble("syed"))));
            bdMap[0].put("yzlbl",AlgorithmUtil.getRealTimeImplementRate(bdilRs.getDouble("syed"),bdilRs.getDouble("ze")));
        }else{
            bdMap[0].put("yzlje",  FormatUtil.DmFormat(0));
            bdMap[0].put("yzlbl",AlgorithmUtil.getRealTimeImplementRate(0,0));
        }
        budgetPs[1].setTitle("预算执行");
        budgetPs[1].setData(bdMap);
        budgetPs[1].setId("yszx");


        return JSON.toJSONString(budgetPs);
    }

    /**
     * 预算核批
     * @param param
     * @return
     */
    @POST
    @Path("/getBudgetApproval")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetApproval(Map<String,Object> param){

        if(param.containsKey("dw") && param.get("dw") instanceof Boolean && (Boolean)param.get("dw") == false)
            return JSON.toJSONString(null);

        Integer pageNum = 1;
        Integer pageSize = 5;
        boolean state = true;
        if(param.containsKey("pageNum") && param.get("pageNum") instanceof Integer &&(Integer)param.get("pageNum")>0)
            pageNum = (Integer) param.get("pageNum");
        if(param.containsKey("pageSize") && param.get("pageSize") instanceof Integer && (Integer)param.get("pageSize")>0)
            pageSize = (Integer) param.get("pageSize");
        if(param.containsKey("state") && param.get("state") instanceof Boolean )
            state = (boolean) param.get("state");
        int nd = getNd(param);
        String dw = "2";
        if(param.containsKey("dw") && param.get("dw") instanceof Integer){
            dw =  AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }
        RecordSet data = DataBaseUtil.getBudAppData(pageNum, pageSize,nd, dw,state, 1);
        Map<String,Object>[] map = new HashMap[data.getArray().size()];
        int i = 0;
        while (data.next()){
            map[i] = new HashMap<String,Object>();
            map[i].put("xmmc",data.getString("xmmin"));
            map[i].put("yszje",data.getString("yszje"));
            map[i].put("cjsj",data.getString("cjsj"));
            i++;
        }
        com.api.gbhg.zengping.page.PageInfo<String> pageInfo=new com.api.gbhg.zengping.page.PageInfo<>();
        int cut = DataBaseUtil.getBudAppCount(nd,dw,state,1);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(cut);
        pageInfo.setList(map);

        return JSON.toJSONString(pageInfo);
    }



    /**
     * 初始化
     * @param param
     */
    private void init(Map<String, Object> param) {
        //实体类
        paIf = new Info[4];
        //年份
        setNd(param);
        //单位
        setDw(param);
    }



    public void setNd(Map<String, Object> param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        if(param.containsKey("nd") && param.get("nd") instanceof Integer ) {
            this.nd = param.get("nd").toString();
        }else {
            Date date = new Date();
            this.nd = sdf.format(date.getTime());
        }
    }


    /**
     * 给预算执行率赋值
     * @return
     */
    public void setImplementRate(){
        RecordSet rs = DataBaseUtil.getTotalImplement(nd,dwId,1);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("ze"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("ze"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[0] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","down");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1",RealTime);
        paIf[0].setTitle("预算执行率");
        paIf[0].setData(map);
        paIf[0].setId("1");

    }


    /**
     * 给中央财政拨款赋值
     * @return
     */
    public void setCenterAppia(){
        RecordSet rs = DataBaseUtil.getAppropriationData(nd,dwId,"1",1);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[1] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","down");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","80");
        map[2].put("zxl2","60");
        map[2].put("state","down");
        paIf[1].setData(map);
        paIf[1].setTitle("中央财政拨款");
        paIf[1].setId("2");
    }


    /**
     * 给地方财政拨款赋值
     * @return
     */
    public void setlocalAppia(){
        RecordSet rs = DataBaseUtil.getAppropriationData(nd,dwId,"3",1);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[2] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","up");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","70");
        map[2].put("zxl2","79");
        map[2].put("state","up");
        paIf[2].setData(map);
        paIf[2].setTitle("地方财政拨款");
        paIf[2].setId("3");
    }


    /**
     * 给中央转移支付赋值
     * @return
     */
    public void setCenterTransfer(){
        RecordSet rs = DataBaseUtil.getAppropriationData(nd,dwId,"2",1);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[3] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","up");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","70");
        map[2].put("zxl2","79");
        map[2].put("state","down");
        paIf[3].setData(map);
        paIf[3].setTitle("中央转移支付");
        paIf[3].setId("4");
    }

    public int getNd(Map<String,Object> param){
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }


}
